/*
 * Copyright (c) 2009-2010 HIT Microelectronic Center
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * Authors: Gou Pengfei
 *
 * Date: Aug. 2010
 *
 */

#include <iostream>
#include <set>
#include <string>
#include <sstream>

#include "base/cprintf.hh"
#include "base/trace.hh"
#include "config/the_isa.hh"
#include "cpu/edge/atomic/base_atomic_dyn_inst.hh"
#include "cpu/exetrace.hh"
#include "mem/request.hh"
#include "sim/faults.hh"

#define NOHASH
#ifndef NOHASH

#include "base/hashmap.hh"

unsigned int MyHashFunc(const BaseAtomicEdgeDynInst *addr)
{
    unsigned a = (unsigned)addr;
    unsigned hash = (((a >> 14) ^ ((a >> 2) & 0xffff))) & 0x7FFFFFFF;

    return hash;
}

typedef m5::hash_map<const BaseAtomicEdgeDynInst *, const BaseAtomicEdgeDynInst *, MyHashFunc>
my_hash_t;

my_hash_t thishash;
#endif

template <class Impl>
BaseAtomicEdgeDynInst<Impl>::BaseAtomicEdgeDynInst(EDGEStaticInstPtr _staticInst,
                               Addr inst_PC, Addr inst_NPC,
                               Addr pred_PC, Addr pred_NPC,
                               InstSeqNum seq_num, ImplCPU *cpu, TheISA::BlockStatus blockstatus)
  : staticInst(_staticInst), traceData(NULL), cpu(cpu)
{
    seqNum = seq_num;

    PC = inst_PC;
    nextPC = inst_NPC;
    nextNPC = nextPC + sizeof(TheISA::MachInst);
    predPC = pred_PC;
    predNPC = pred_NPC;
    predTaken = false;

   blockStatus = blockstatus;

    initVars();
}

template <class Impl>
BaseAtomicEdgeDynInst<Impl>::BaseAtomicEdgeDynInst(TheISA::ExtMachInst inst,
                               Addr inst_PC, Addr inst_NPC,
                               Addr pred_PC, Addr pred_NPC,
                               InstSeqNum seq_num, ImplCPU *cpu, TheISA::BlockStatus blockstatus)
  : staticInst(inst, inst_PC, blockstatus), traceData(NULL), cpu(cpu)
{
    seqNum = seq_num;
    
    PC = inst_PC;
    nextPC = inst_NPC;
    nextNPC = nextPC + sizeof(TheISA::MachInst);
    predPC = pred_PC;
    predNPC = pred_NPC;
    predTaken = false;

    blockStatus = blockstatus;

    initVars();
}

//template <class Impl>
//BaseAtomicEdgeDynInst<Impl>::BaseAtomicEdgeDynInst(EDGEStaticInstPtr &_staticInst)
//    : staticInst(_staticInst), traceData(NULL)
//{
//    seqNum = 0;
//    initVars();
//}

template <class Impl>
void
BaseAtomicEdgeDynInst<Impl>::initVars()
{
    memData = NULL;
    effAddr = 0;
    effAddrValid = false;
    physEffAddr = 0;
    memReq = NULL;

    isUncacheable = false;
    reqMade = false;

    status.reset();

    eaCalcDone = false;
    memOpDone = false;

    // Eventually make this a parameter.
    threadNumber = 0;

    // Also make this a parameter, or perhaps get it from xc or cpu.
    asid = 0;

    // Initialize the fault to be NoFault.
    fault = NoFault;
    instBlock = NULL;

#ifndef NDEBUG
    ++cpu->instCount;

    if (cpu->instCount > 1920) {
#ifdef DEBUG
        //cpu->dumpInsts();
        dumpSNList();
#endif
        assert(cpu->instCount <= 1920);
    }

    DPRINTF(DynInst,
        "DynInst: [sn:%lli] Instruction created. Instcount for %s = %i\n",
        seqNum, cpu->name(), cpu->instCount);
#endif

#ifdef DEBUG
    cpu->snList.insert(seqNum);
#endif
}

template <class Impl>
BaseAtomicEdgeDynInst<Impl>::~BaseAtomicEdgeDynInst()
{
    if (memData) {
        delete [] memData;
    }

    if (traceData) {
        delete traceData;
    }

    if (memReq) {
        delete memReq;
    }
    
    fault = NoFault;
    instBlock = NULL;

#ifndef NDEBUG
    --cpu->instCount;

    DPRINTF(DynInst,
        "DynInst: [sn:%lli] Instruction destroyed. Instcount for %s = %i\n",
        seqNum, cpu->name(), cpu->instCount);
#endif
#ifdef DEBUG
    cpu->snList.erase(seqNum);
#endif
}

#ifdef DEBUG
template <class Impl>
void
BaseAtomicEdgeDynInst<Impl>::dumpSNList()
{
    std::set<InstSeqNum>::iterator sn_it = cpu->snList.begin();

    int count = 0;
    while (sn_it != cpu->snList.end()) {
        cprintf("%i: [sn:%lli] not destroyed\n", count, (*sn_it));
        count++;
        sn_it++;
    }
}
#endif

template <class Impl>
void
BaseAtomicEdgeDynInst<Impl>::dump()
{
    cprintf("T%d : %#08d `", threadNumber, PC);
    std::cout << staticInst->disassemble(PC);
    cprintf("'\n");
}

template <class Impl>
void
BaseAtomicEdgeDynInst<Impl>::dump(std::string &outstring)
{
    std::ostringstream s;
    s << "T" << threadNumber << " : 0x" << PC << " "
      << staticInst->disassemble(PC);

    outstring = s.str();
}
